﻿using LibraryManagement.Custom_Controls;
using LibraryManagement.Custom_Controls.Data_Grid_View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Membership
{
    public partial class MemberUpdation : libForm
    {
        private LibManagementEntities context;
        private List<MemberDetail> listMembers;
        private MemberAdd OMemberAdd;
        private int id;
        private MemberDetail OMemberDetail;
        private object MemberId;
        public MemberUpdation()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MemberUpdation_Load(object sender, EventArgs e)
        {
            context = new LibManagementEntities();
            dgvMembers.Columns.Add(new libDataGridViewColumnDelete());
            dgvMembers.Columns.Add(new libDataGridViewColumnEdit());
            SetDataGridView();
        }

        private void SetDataGridView()
        {
            var context = new LibManagementEntities();
            dgvMembers.DataSource = context.MemberDetails.ToList();
        }

        private void dgvMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                id = Convert.ToInt32(dgvMembers.Rows[e.RowIndex].Cells["Id"].Value);
                //id = Convert.ToInt32(context.MemberDetails.Single(x => x.MemberEnrollmentNo == MemberId).Id);
                if (e.ColumnIndex == 1)
                {
                    OMemberAdd = new MemberAdd(id, MemberAdd.Redirection.Edit);
                    OMemberAdd.ShowDialog();
                }
                else if (e.ColumnIndex == 0)
                {
                    OMemberDetail = context.MemberDetails.Single(x => x.Id == id);
                    context.MemberDetails.Remove(OMemberDetail);
                    context.SaveChanges();
                }
                SetDataGridView();
            }
        }
    }



    //if (e.RowIndex != -1)
    //        {
    //            MemberId = dgvMembers.Rows[e.RowIndex].Cells["MemberId"].Value;
    //            id = Convert.ToInt32(context.MemberDetails.Single(x => x.MemberId == MemberId).Id);
    //            if (e.ColumnIndex == 1)
    //            {
    //                OMemberAdd = new MemberAdd(id, MemberAdd.Redirection.Edit);
    //                OMemberAdd.ShowDialog();
    //            }
    //            else if (e.ColumnIndex == 0)
    //            {
    //                OMemberDetail = context.MemberDetails.Single(x => x.Id == id);
    //                context.MemberDetails.Remove(OMemberDetail);
    //                context.SaveChanges();
    //            }
    //            SetDataGridView();
}
